/*
 * Copyright 2009, Qys Creations 
 * 
 *  This file is part of Chameleon DB.
 *
 *  Chameleon DB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chameleon DB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chameleon DB.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */


import java.util.*;

public class db_table {
	public String sName;
	public long lGroup;
	public long lDataGroup;
	public long lIndexGroup;
	public int iRowSize;
	public ArrayList<db_index> oIndex = new ArrayList<db_index>();
	public ArrayList<db_column> oColumns = new ArrayList<db_column>();
	
	db_table(String sTableName, long lGroupVal, long lDataGroupVal, long lIndexGroupVal, ArrayList<db_index> oIndexVal, ArrayList<db_column> oCols){
		sName = sTableName;
		lGroup = lGroupVal;
		lDataGroup = lDataGroupVal;
		lIndexGroup = lIndexGroupVal;
		oIndex = oIndexVal;
		oColumns = oCols;
		iRowSize = 4; // Row status code
		for (int i = 0; i < oColumns.size(); i++) {
			iRowSize += oColumns.get(i).lLength;
			if (oColumns.get(i).lType == db_util.DB_DATATYPE_VARCHAR)
				iRowSize += 4;
		}			
		
	}


	public int iColumnPosition(String sColumnName) {
		sColumnName = sColumnName.toLowerCase();
		int iPos = 4; // Row status code
		for (int i = 0; i < oColumns.size(); i++) {
			if (sColumnName.equals(oColumns.get(i).sName.toLowerCase()))
					break;
			iPos += oColumns.get(i).lLength;
			if (oColumns.get(i).lType == db_util.DB_DATATYPE_VARCHAR)
				iPos += 4;
		}		
		return iPos;
	}
	public db_column oGetColumn(String sColumnName) {
		sColumnName = sColumnName.toLowerCase();
		for (int i = 0; i < oColumns.size(); i++) {
			if (sColumnName.equals(oColumns.get(i).sName.toLowerCase()))
				return oColumns.get(i);
		}
		return null;
	}
}
